package cc.sjfx.sharedoc.dialog;

import cc.sjfx.sharedoc.R;
import cc.sjfx.sharedoc.bean.MajorPickerBean;
import cc.sjfx.sharedoc.bean.SetInfoBean;
import cc.sjfx.sharedoc.cache.UserCache;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class SetUserInfoDialog extends Dialog implements android.view.View.OnClickListener,OnCheckedChangeListener,DialogListener<MajorPickerBean>{
	MyDialogFrame frame;
	EditText signEdit;
	RadioGroup sexRG;
	RadioButton sexMaleRB;
	RadioButton sexFemaleRB;
	EditText majorEdit;
	Button submit;
	private int sex=1;
	MajorPickerDialog majorPickerDialog;
	Context context;
	MajorPickerBean majorPickerBean;
	public SetUserInfoDialog(Context context) {
		super(context, R.style.MyDialog);
		this.context=context;
		setContentView(R.layout.dialog_setmyinfo);
		setCanceledOnTouchOutside(false);
		signEdit = (EditText)findViewById(R.id.signEdit);
		sexRG = (RadioGroup)findViewById(R.id.radioGroup1);
		sexMaleRB = (RadioButton)findViewById(R.id.radio0);
		sexFemaleRB = (RadioButton) findViewById(R.id.radio1);
		frame=(MyDialogFrame)findViewById(R.id.MDF);
		frame.setTitle("设置");
		submit = (Button)findViewById(R.id.button1);
		majorEdit=(EditText)findViewById(R.id.majorEdit);
		majorEdit.setText(UserCache.user.getMid()==0?"":UserCache.user.getMajor());
		majorEdit.setOnClickListener(this);
		setCanceledOnTouchOutside(false);
		sexRG.setOnCheckedChangeListener(this);
		signEdit.setText(UserCache.user.getSign());
		if (UserCache.user.getSex() == 1) {
			sexMaleRB.setChecked(true);
			sex = 1;
		} else if (UserCache.user.getSex() == 2) {
			sexFemaleRB.setChecked(true);
			sex = 2;
		}
	}
	public void setDialogListener(final DialogListener<SetInfoBean> dialogListener) {
		submit.setOnClickListener(new android.view.View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SetInfoBean bean=new SetInfoBean(signEdit.getText().toString(), sex, majorPickerBean);
				dialogListener.buttonClick(bean);
			}
		});
	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.majorEdit:
			if(majorPickerDialog==null){
				majorPickerDialog= new MajorPickerDialog(context);
				majorPickerDialog.setDialogListener(this);
			}
			majorPickerDialog.show();
			break;
		default:
			break;
		}
	}
	@Override
	public void onCheckedChanged(RadioGroup arg0, int arg1) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.radioGroup1:
			setRadioGroup1Checked(arg1);
			break;
		default:
			break;
		}
	}
	private void setRadioGroup1Checked(int arg1) {
		// TODO Auto-generated method stub
		switch (arg1) {
		case R.id.radio0:
			sex = 1;
			break;
		case R.id.radio1:
			sex = 2;
			break;
		}
	}
	@Override
	public void buttonClick(MajorPickerBean t) {
		// TODO Auto-generated method stub
		majorPickerBean=t;
		majorEdit.setText(t.getMajor());
	}
}
